import { ILearnState, ICommonAction } from '@/interface/store';
import { learnWordData } from '@/api/word';


const store: IStore = {
  nameSpace: 'learn',
  state:{
    learnList: []
  },
  actions:{
    SET_WORD_LEARN_LIST(state: ILearnState, action: ICommonAction){
      return {
        ...state,
        learnList: action.payload
      }
    },
  },
  // 异步方法 优化redux-thunk的异步写法
  mutations: {
    async getLearnList(dispatch: Function, payload: ILearnWordPayload) {
      const { data } = await learnWordData(payload);
      dispatch({
        type: 'SET_WORD_LEARN_LIST',
        payload: data
      });
    }
  },
  actionNames:{
    SET_WORD_LEARN_LIST: 'SET_WORD_LEARN_LIST'
  }
};

let actionNames: any = {};

for(let key in store.actions){
  actionNames[key] = key
}

store.actionNames = actionNames;

export default store;